//
//  WLAlertView.m
//  WanLian
//
//  Created by muzhuang on 2018/11/19.
//  Copyright © 2018年 wanlian. All rights reserved.
//

#import "WLAlertView.h"

@implementation WLAlertView

- (id)initWithTitle:(NSString *)title message:(NSString *)message cancelBtnTitle:(NSString *)cancelTitle goBtnTitle:(NSString *)goTitle
{
    self = [super init];
    if (self) {
        CGFloat width = 300.0;//WIDTH_SCREEN_WL-WLScaleX(72);
        _alert = [[UIView alloc]initWithFrame:CGRectMake((WIDTH_SCREEN_WL-width)/2.0, (HEIGHT_SCREEN_WL-170)/2.0, width, 170)];
        _alert.backgroundColor = wlUIColorRGB(0xFAFAFA);
        [self addSubview:_alert];
        _alert.layer.cornerRadius = 5;
        _alert.layer.masksToBounds = YES;
        
        self.frame = [[UIScreen mainScreen]bounds];
        self.backgroundColor = wlRGBACOLOR(0, 0, 0, 0.5);
        
        UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, 25, width-10, 25)];
        titleLabel.text = title;
        titleLabel.textColor = wlBlackColor;
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.font = [WLCommonTool fontTypePingFangSCMedium:17];
        [_alert addSubview:titleLabel];
        
        UILabel *msgLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, 50+20, width-10, 25)];
        msgLabel.text = message;
        msgLabel.textColor = wlDarkGrayColor;
        msgLabel.textAlignment = NSTextAlignmentCenter;
//        msgLabel.numberOfLines = 0;
        msgLabel.font = [WLCommonTool fontTypePingFangSCRegular:14];
        [_alert addSubview:msgLabel];
        
        UIView *henV = [[UIView alloc]initWithFrame:CGRectMake(0, 170-45, width, 1)];
        henV.backgroundColor = wlUIColorRGB(0xF2F2F2);
        [_alert addSubview:henV];
        
        if (cancelTitle.length>0)
        {
            float btnWidth = width/2.0;
            UIButton *cancelBtn = [[UIButton alloc] initWithFrame:CGRectMake(10, 170-44, btnWidth-20, 44)];
            cancelBtn.titleLabel.font = [WLCommonTool fontTypePingFangSCMedium:17];
            [cancelBtn setTitle:cancelTitle forState:UIControlStateNormal];
            [cancelBtn setTitleColor:wlUnderlineColor forState:UIControlStateNormal];
            [cancelBtn addTarget:self action:@selector(cancelClick) forControlEvents:UIControlEventTouchUpInside];
            [_alert addSubview:cancelBtn];
            
            UIView *verticaline = [[UIView alloc]initWithFrame:CGRectMake(width/2.0, 170-37, 1, 30)];
            verticaline.backgroundColor = wlUIColorRGB(0xF2F2F2);
            [_alert addSubview:verticaline];
            
            UIButton *sureBtn = [[UIButton alloc] initWithFrame:CGRectMake(btnWidth+10, 170-44, btnWidth-20, WLScaleX(44))];
            sureBtn.titleLabel.font = [WLCommonTool fontTypePingFangSCMedium:17];
            [sureBtn setTitle:goTitle forState:UIControlStateNormal];
            [sureBtn setTitleColor:wlUIColorRGB(0x29A8FA) forState:UIControlStateNormal];
            [sureBtn addTarget:self action:@selector(sureClick) forControlEvents:UIControlEventTouchUpInside];
            [_alert addSubview:sureBtn];
        }
        else
        {
            UIButton *sureBtn = [[UIButton alloc] initWithFrame:CGRectMake(10, 170-44, width-20, WLScaleX(44))];
            sureBtn.titleLabel.font = [WLCommonTool fontTypePingFangSCMedium:17];
            [sureBtn setTitle:goTitle forState:UIControlStateNormal];
            [sureBtn setTitleColor:wlUIColorRGB(0x29A8FA) forState:UIControlStateNormal];
            [sureBtn addTarget:self action:@selector(sureClick) forControlEvents:UIControlEventTouchUpInside];
            [_alert addSubview:sureBtn];
        }
        
//        UIButton *sureBtn = [[UIButton alloc] initWithFrame:CGRectMake(btnWidth+10, 170-44, btnWidth-20, WLScaleX(44))];
//        sureBtn.titleLabel.font = [WLCommonTool fontTypePingFangSCMedium:17];
//        [sureBtn setTitle:goTitle forState:UIControlStateNormal];
//        [sureBtn setTitleColor:wlUIColorRGB(0x29A8FA) forState:UIControlStateNormal];
//        [sureBtn addTarget:self action:@selector(sureClick) forControlEvents:UIControlEventTouchUpInside];
//        [_alert addSubview:sureBtn];
    }
    return self;
}

- (void)cancelClick
{
    if (self.cancelAction) {
        self.cancelAction();
    }
    [self hide];
}

- (void)sureClick
{
    if (self.okAction) {
        self.okAction();
    }
    [self hide];
}

- (void)show
{
    if (self.superview==nil) {
        UIWindow *window =  [[UIApplication sharedApplication].windows firstObject];
        [window addSubview:self];
    }
    
    self.alpha = 0;
    _alert.transform = CGAffineTransformScale(_alert.transform,0.1,0.1);
    WLWkSelf
    [UIView animateWithDuration:0.25 animations:^{
        weakSelf.alert.transform = CGAffineTransformIdentity;
        self.alpha = 1;
    }];
}

- (void)hide
{
    if (self.superview) {
        WLWkSelf
        [UIView animateWithDuration:0.25 animations:^{
            weakSelf.alert.transform = CGAffineTransformScale(weakSelf.alert.transform,0.1,0.1);
            self.alpha = 0;
        } completion:^(BOOL finished) {
            [self removeFromSuperview];
        }];
    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
